package com.vhuomai.product.label.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.vhuomai.product.label.entity.BaseLabel;
import com.vhuomai.product.label.mapper.LabelMapper;
import com.vhuomai.product.label.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private LabelMapper labelMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createLabel(List <BaseLabel> productLabel) {
        productLabel.forEach(label -> {
            label.setCreateTime(LocalDateTime.now());
            label.setUpdateTime(LocalDateTime.now());
        });
        labelMapper.insertAll(productLabel);
    }

    @Override
    public void updateLabel(BaseLabel productLabel) {
        BaseLabel label = labelMapper.findOne(productLabel.getId());
        AssertUtils.notNull(label, "数据校检出错");
        productLabel.setCreateTime(label.getCreateTime());
        productLabel.setUpdateTime(LocalDateTime.now());
        labelMapper.update(productLabel);
    }

    @Override
    public void deleteLabel(Integer id) {
        labelMapper.deleteOne(id);
    }

    @Override
    public List <BaseLabel> findLabels(Integer categoryId) {
        return labelMapper.findByCategoryId(categoryId);
    }
}
